In [1]:
import numpy as np
import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LinearRegression,SGDRegressor

from sklearn.preprocessing import PolynomialFeatures,StandardScaler

from sklearn.metrics import r2_score

from sklearn.pipeline import Pipeline
In [2]:
X = 6 * np.random.rand(200, 1) - 3
y = 0.8 * X**2 + 0.9 * X + 2 + np.random.randn(200, 1)

# y = 0.8x^2 + 0.9x + 2
In [3]:
plt.plot(X, y,'b.')
plt.xlabel("X")
plt.ylabel("y")
plt.show()
In [4]:
# Train test split
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=2)
In [5]:
# Applying linear regression
lr = LinearRegression()
In [6]:
lr.fit(X_train,y_train)
Out[6]:
LinearRegression()
In [7]:
y_pred = lr.predict(X_test)
r2_score(y_test,y_pred)
Out[7]:
0.16130669964603517
In [8]:
plt.plot(X_train,lr.predict(X_train),color='r')
plt.plot(X, y, "b.")
plt.xlabel("X")
plt.ylabel("y")
plt.show()
In [9]:
# Applying Polynomial Linear Regression
# degree 2
poly = PolynomialFeatures(degree=2,include_bias=True)

X_train_trans = poly.fit_transform(X_train)
X_test_trans = poly.transform(X_test)
In [10]:
print(X_train[0])
print(X_train_trans[0])
[-0.88762061]
[ 1.         -0.88762061  0.78787035]
In [11]:
# include_bias parameter
In [12]:
lr = LinearRegression()
lr.fit(X_train_trans,y_train)
Out[12]:
LinearRegression()
In [13]:
y_pred = lr.predict(X_test_trans)
In [14]:
r2_score(y_test,y_pred)
Out[14]:
0.8332188324158399
In [15]:
print(lr.coef_)
print(lr.intercept_)
[[0.         0.89296931 0.78439465]]
[1.99965238]
In [16]:
X_new=np.linspace(-3, 3, 200).reshape(200, 1)
X_new_poly = poly.transform(X_new)
y_new = lr.predict(X_new_poly)
In [17]:
plt.plot(X_new, y_new, "r-", linewidth=2, label="Predictions")
plt.plot(X_train, y_train, "b.",label='Training points')
plt.plot(X_test, y_test, "g.",label='Testing points')
plt.xlabel("X")
plt.ylabel("y")
plt.legend()
plt.show()
In [18]:
def polynomial_regression(degree):
    X_new=np.linspace(-3, 3, 100).reshape(100, 1)
    X_new_poly = poly.transform(X_new)

    polybig_features = PolynomialFeatures(degree=degree, include_bias=False)
    std_scaler = StandardScaler()
    lin_reg = LinearRegression()
    polynomial_regression = Pipeline([
            ("poly_features", polybig_features),
            ("std_scaler", std_scaler),
            ("lin_reg", lin_reg),
        ])
    polynomial_regression.fit(X, y)
    y_newbig = polynomial_regression.predict(X_new)
    plt.plot(X_new, y_newbig,'r', label="Degree " + str(degree), linewidth=2)

    plt.plot(X_train, y_train, "b.", linewidth=3)
    plt.plot(X_test, y_test, "g.", linewidth=3)
    plt.legend(loc="upper left")
    plt.xlabel("X")
    plt.ylabel("y")
    plt.axis([-3, 3, 0, 10])
    plt.show()
In [19]:
polynomial_regression(350)
C:\Users\91842\anaconda3\lib\site-packages\numpy\lib\nanfunctions.py:1544: RuntimeWarning: overflow encountered in multiply
  sqr = np.multiply(arr, arr, out=arr)
C:\Users\91842\anaconda3\lib\site-packages\numpy\core\fromnumeric.py:87: RuntimeWarning: overflow encountered in reduce
  return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
In [20]:
poly.powers_
Out[20]:
array([[0],
       [1],
       [2]], dtype=int64)
In [21]:
# Applying Gradient Descent

poly = PolynomialFeatures(degree=2)

X_train_trans = poly.fit_transform(X_train)
X_test_trans = poly.transform(X_test)

sgd = SGDRegressor(max_iter=100)
sgd.fit(X_train_trans,y_train)

X_new=np.linspace(-2.9, 2.8, 200).reshape(200, 1)
X_new_poly = poly.transform(X_new)
y_new = sgd.predict(X_new_poly)

y_pred = sgd.predict(X_test_trans)

plt.plot(X_new, y_new, "r-", linewidth=2, label="Predictions " + str(round(r2_score(y_test,y_pred),2)))
plt.plot(X_train, y_train, "b.",label='Training points')
plt.plot(X_test, y_test, "g.",label='Testing points')
plt.xlabel("X")
plt.ylabel("y")
plt.legend()
plt.show()
C:\Users\91842\anaconda3\lib\site-packages\sklearn\utils\validation.py:63: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  return f(*args, **kwargs)
In [22]:
# 3D polynomial regression
x = 7 * np.random.rand(100, 1) - 2.8
y = 7 * np.random.rand(100, 1) - 2.8

z = x**2 + y**2 + 0.2*x + 0.2*y + 0.1*x*y +2 + np.random.randn(100, 1)
# z = x^2 + y^2 + 0.2x + 0.2y + 0.1xy + 2
In [23]:
import plotly.express as px
df = px.data.iris()
fig = px.scatter_3d(df, x=x.ravel(), y=y.ravel(), z=z.ravel())
fig.show()
In [24]:
lr = LinearRegression()
lr.fit(np.array([x,y]).reshape(100,2),z)

x_input = np.linspace(x.min(), x.max(), 10)
y_input = np.linspace(y.min(), y.max(), 10)
xGrid, yGrid = np.meshgrid(x_input,y_input)

final = np.vstack((xGrid.ravel().reshape(1,100),yGrid.ravel().reshape(1,100))).T

z_final = lr.predict(final).reshape(10,10)
In [25]:
import plotly.graph_objects as go

fig = px.scatter_3d(df, x=x.ravel(), y=y.ravel(), z=z.ravel())

fig.add_trace(go.Surface(x = x_input, y = y_input, z =z_final ))

fig.show()
In [26]:
X_multi = np.array([x,y]).reshape(100,2)
X_multi.shape
Out[26]:
(100, 2)
In [27]:
poly = PolynomialFeatures(degree=30)
X_multi_trans = poly.fit_transform(X_multi)
In [28]:
print("Input",poly.n_input_features_)
print("Ouput",poly.n_output_features_)
print("Powers\n",poly.powers_)
Input 2
Ouput 496
Powers
 [[ 0  0]
 [ 1  0]
 [ 0  1]
 [ 2  0]
 [ 1  1]
 [ 0  2]
 [ 3  0]
 [ 2  1]
 [ 1  2]
 [ 0  3]
 [ 4  0]
 [ 3  1]
 [ 2  2]
 [ 1  3]
 [ 0  4]
 [ 5  0]
 [ 4  1]
 [ 3  2]
 [ 2  3]
 [ 1  4]
 [ 0  5]
 [ 6  0]
 [ 5  1]
 [ 4  2]
 [ 3  3]
 [ 2  4]
 [ 1  5]
 [ 0  6]
 [ 7  0]
 [ 6  1]
 [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 2 14]
 [ 1 15]
 [ 0 16]
 [17  0]
 [16  1]
 [15  2]
 [14  3]
 [13  4]
 [12  5]
 [11  6]
 [10  7]
 [ 9  8]
 [ 8  9]
 [ 7 10]
 [ 6 11]
 [ 5 12]
 [ 4 13]
 [ 3 14]
 [ 2 15]
 [ 1 16]
 [ 0 17]
 [18  0]
 [17  1]
 [16  2]
 [15  3]
 [14  4]
 [13  5]
 [12  6]
 [11  7]
 [10  8]
 [ 9  9]
 [ 8 10]
 [ 7 11]
 [ 6 12]
 [ 5 13]
 [ 4 14]
 [ 3 15]
 [ 2 16]
 [ 1 17]
 [ 0 18]
 [19  0]
 [18  1]
 [17  2]
 [16  3]
 [15  4]
 [14  5]
 [13  6]
 [12  7]
 [11  8]
 [10  9]
 [ 9 10]
 [ 8 11]
 [ 7 12]
 [ 6 13]
 [ 5 14]
 [ 4 15]
 [ 3 16]
 [ 2 17]
 [ 1 18]
 [ 0 19]
 [20  0]
 [19  1]
 [18  2]
 [17  3]
 [16  4]
 [15  5]
 [14  6]
 [13  7]
 [12  8]
 [11  9]
 [10 10]
 [ 9 11]
 [ 8 12]
 [ 7 13]
 [ 6 14]
 [ 5 15]
 [ 4 16]
 [ 3 17]
 [ 2 18]
 [ 1 19]
 [ 0 20]
 [21  0]
 [20  1]
 [19  2]
 [18  3]
 [17  4]
 [16  5]
 [15  6]
 [14  7]
 [13  8]
 [12  9]
 [11 10]
 [10 11]
 [ 9 12]
 [ 8 13]
 [ 7 14]
 [ 6 15]
 [ 5 16]
 [ 4 17]
 [ 3 18]
 [ 2 19]
 [ 1 20]
 [ 0 21]
 [22  0]
 [21  1]
 [20  2]
 [19  3]
 [18  4]
 [17  5]
 [16  6]
 [15  7]
 [14  8]
 [13  9]
 [12 10]
 [11 11]
 [10 12]
 [ 9 13]
 [ 8 14]
 [ 7 15]
 [ 6 16]
 [ 5 17]
 [ 4 18]
 [ 3 19]
 [ 2 20]
 [ 1 21]
 [ 0 22]
 [23  0]
 [22  1]
 [21  2]
 [20  3]
 [19  4]
 [18  5]
 [17  6]
 [16  7]
 [15  8]
 [14  9]
 [13 10]
 [12 11]
 [11 12]
 [10 13]
 [ 9 14]
 [ 8 15]
 [ 7 16]
 [ 6 17]
 [ 5 18]
 [ 4 19]
 [ 3 20]
 [ 2 21]
 [ 1 22]
 [ 0 23]
 [24  0]
 [23  1]
 [22  2]
 [21  3]
 [20  4]
 [19  5]
 [18  6]
 [17  7]
 [16  8]
 [15  9]
 [14 10]
 [13 11]
 [12 12]
 [11 13]
 [10 14]
 [ 9 15]
 [ 8 16]
 [ 7 17]
 [ 6 18]
 [ 5 19]
 [ 4 20]
 [ 3 21]
 [ 2 22]
 [ 1 23]
 [ 0 24]
 [25  0]
 [24  1]
 [23  2]
 [22  3]
 [21  4]
 [20  5]
 [19  6]
 [18  7]
 [17  8]
 [16  9]
 [15 10]
 [14 11]
 [13 12]
 [12 13]
 [11 14]
 [10 15]
 [ 9 16]
 [ 8 17]
 [ 7 18]
 [ 6 19]
 [ 5 20]
 [ 4 21]
 [ 3 22]
 [ 2 23]
 [ 1 24]
 [ 0 25]
 [26  0]
 [25  1]
 [24  2]
 [23  3]
 [22  4]
 [21  5]
 [20  6]
 [19  7]
 [18  8]
 [17  9]
 [16 10]
 [15 11]
 [14 12]
 [13 13]
 [12 14]
 [11 15]
 [10 16]
 [ 9 17]
 [ 8 18]
 [ 7 19]
 [ 6 20]
 [ 5 21]
 [ 4 22]
 [ 3 23]
 [ 2 24]
 [ 1 25]
 [ 0 26]
 [27  0]
 [26  1]
 [25  2]
 [24  3]
 [23  4]
 [22  5]
 [21  6]
 [20  7]
 [19  8]
 [18  9]
 [17 10]
 [16 11]
 [15 12]
 [14 13]
 [13 14]
 [12 15]
 [11 16]
 [10 17]
 [ 9 18]
 [ 8 19]
 [ 7 20]
 [ 6 21]
 [ 5 22]
 [ 4 23]
 [ 3 24]
 [ 2 25]
 [ 1 26]
 [ 0 27]
 [28  0]
 [27  1]
 [26  2]
 [25  3]
 [24  4]
 [23  5]
 [22  6]
 [21  7]
 [20  8]
 [19  9]
 [18 10]
 [17 11]
 [16 12]
 [15 13]
 [14 14]
 [13 15]
 [12 16]
 [11 17]
 [10 18]
 [ 9 19]
 [ 8 20]
 [ 7 21]
 [ 6 22]
 [ 5 23]
 [ 4 24]
 [ 3 25]
 [ 2 26]
 [ 1 27]
 [ 0 28]
 [29  0]
 [28  1]
 [27  2]
 [26  3]
 [25  4]
 [24  5]
 [23  6]
 [22  7]
 [21  8]
 [20  9]
 [19 10]
 [18 11]
 [17 12]
 [16 13]
 [15 14]
 [14 15]
 [13 16]
 [12 17]
 [11 18]
 [10 19]
 [ 9 20]
 [ 8 21]
 [ 7 22]
 [ 6 23]
 [ 5 24]
 [ 4 25]
 [ 3 26]
 [ 2 27]
 [ 1 28]
 [ 0 29]
 [30  0]
 [29  1]
 [28  2]
 [27  3]
 [26  4]
 [25  5]
 [24  6]
 [23  7]
 [22  8]
 [21  9]
 [20 10]
 [19 11]
 [18 12]
 [17 13]
 [16 14]
 [15 15]
 [14 16]
 [13 17]
 [12 18]
 [11 19]
 [10 20]
 [ 9 21]
 [ 8 22]
 [ 7 23]
 [ 6 24]
 [ 5 25]
 [ 4 26]
 [ 3 27]
 [ 2 28]
 [ 1 29]
 [ 0 30]]
In [29]:
X_multi_trans.shape
Out[29]:
(100, 496)
In [30]:
lr = LinearRegression()
lr.fit(X_multi_trans,z)
Out[30]:
LinearRegression()
In [31]:
X_test_multi = poly.transform(final)
In [32]:
z_final = lr.predict(X_multi_trans).reshape(10,10)
In [33]:
fig = px.scatter_3d(x=x.ravel(), y=y.ravel(), z=z.ravel())

fig.add_trace(go.Surface(x = x_input, y = y_input, z =z_final))

fig.update_layout(scene = dict(zaxis = dict(range=[0,35])))

fig.show()